package org.dicker.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.ManagerBiz;
import org.dicker.biz.impl.ManagerBizImpl;
import org.dicker.entity.ManagerInfo;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseVoid;

import cn.hutool.captcha.CircleCaptcha;

@Controller("/manager")
public class ManagerController{
	
	private ManagerBiz managerBiz = new ManagerBizImpl();

	@RequestMapping(value = "/login.do")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name="username") String account,
			@RequestParam(name="rememberme") String rememberme,
			@RequestParam(name="password") String password){
		
		boolean isok = false;
		
		try {
			ManagerInfo managerInfo = managerBiz.selectManagerList(account, password);
			request.getSession().setAttribute("USER_LOGIN", managerInfo);
			
			if(managerInfo!=null) {
				isok = true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isok?"1":"0";
	}

	@RequestMapping(value = "/exit.do")
	@ResponseVoid
	public void exit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		request.getSession().setAttribute("USER_LOGIN", null);
		response.sendRedirect("/big-birds/login.jsp");
	}
	
	@RequestMapping(value = "/verify.do")
	@ResponseBody
	public String verifycode(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name="authCode") String authCode){
		
		CircleCaptcha captcha = (CircleCaptcha) request.getSession().getAttribute("SYSTEM_CODE");
		
		return captcha.verify(authCode)?"1":"0";
	}

}
